package leetcode.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * 144. 二叉树的前序遍历
 */
public class _144 {

    public List<Integer> preorderTraversal(TreeNode root) {

        List<Integer> result = new ArrayList<>();

        if (root == null) {
            return result;
        }

        getChild(root, result);

        System.out.println("result = " + result);
        return result;


    }

    public static void getChild(TreeNode cur, List<Integer> result) {
        if (cur == null) {
            return;
        }
        result.add(cur.val);

        getChild(cur.left, result);
        getChild(cur.right, result);


    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

}
